package com.shishuo.cms.tag;

import com.shishuo.cms.plugin.TagPlugin;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by CrazyCode on 2015/8/6.
 */
@Service
public class TextCutUtils extends TagPlugin {
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        String text = map.get("text").toString();
        String htmltag = "<([^>]*)>";
        text = text.replaceAll(htmltag,"");
        int cut = Integer.parseInt(map.get("cut").toString());
        String append = map.get("append").toString();
        String subString = text.length()>cut?text.substring(0,cut):text;
        StringBuilder stringBuilder = new StringBuilder(subString);
        environment.getOut().write(stringBuilder.append(append).toString());

    }
}
